package com.yfore.jfinal.alidayu.plugin;

import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class AlidayuKit {

	private static TaobaoClient client = null;
	
	/**
	 * 初始化TaobaoClient
	 * @param client
	 * 	TaobaoClient
	 */
	static void init(TaobaoClient client){
		AlidayuKit.client = client;
	}
	
	/**
	 * 发送短信
	 * @param smsFreeSignName
	 * 	短信签名
	 * @param smsParam
	 * 	短信参数
	 * @param recNum
	 * 	接收号码
	 * @param smsTemplateCode
	 * 	短信模板ID
	 * @return
	 * @throws AlidayuSendException 
	 * 发送异常
	 */
	public static String send(String smsFreeSignName,String smsParam,String recNum,String smsTemplateCode) throws AlidayuSendException{
		return send(null, "normal", smsFreeSignName, smsParam, recNum, smsTemplateCode);
	}

	/**
	 * 发送短信
	 * @param extend
	 * 	公共回传参数
	 * @param smsType
	 * 	短信类型
	 * @param smsFreeSignName
	 * 	短信签名
	 * @param smsParam
	 * 	短信参数
	 * @param recNum
	 * 	接收号码
	 * @param smsTemplateCode
	 * 	短信模板ID
	 * @return
	 * @throws AlidayuSendException 
	 * 	发送异常
	 */
	public static String send(String extend,String smsType,String smsFreeSignName,String smsParam,String recNum,String smsTemplateCode) throws AlidayuSendException{
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend(extend);
		req.setSmsType(smsType);
		req.setSmsFreeSignName(smsFreeSignName);
		req.setSmsParamString(smsParam);
		req.setRecNum(recNum);
		req.setSmsTemplateCode(smsTemplateCode);
		try {
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			return rsp.getBody();
		} catch (ApiException e) {
			throw new AlidayuSendException("短信发送异常！",e);
		}
	}
	
}
